AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

/*---------------------------------------------------------
   Name: ENT:Initialize()
---------------------------------------------------------*/
function ENT:Initialize()
	self.Entity:SetModel("models/Items/grenadeAmmo.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:DrawShadow(false)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
	end
	self.OneTime = true
end

/*---------------------------------------------------------
   Name: ENT:Think()
---------------------------------------------------------*/
function ENT:Think()
	if !ValidEntity(self.Owner) then
		self:Remove()
	end
	local tracedata = {}
	tracedata.start = self.Entity:GetPos()
	tracedata.endpos = Vector(self.Entity:GetPos().x, self.Entity:GetPos().y, self.Entity:GetPos().z - 100)
	tracedata.filter = self.Entity
	local tr = util.TraceLine(tracedata)

	if (self.Entity:GetDTBool(0) and self.Entity:WaterLevel() <= 0) then

		local effectdata = EffectData()
			effectdata:SetOrigin(self.Entity:GetPos())
		util.Effect("effect_mad_incendiary_fire", effectdata)

		local pos = self.Entity:GetPos()
		local maxrange = 275
		for k,v in pairs(player.GetAll()) do
			local plpos = v:GetPos()
			local dist = -pos:Distance(plpos)+maxrange
			if (pos:Distance(plpos)<=maxrange) then
				local trace = {}
					trace.start = self.Entity:GetPos()
					trace.endpos = v:GetPos()+Vector(0,0,24)
					trace.filter = { v, self.Entity }
					trace.mask = COLLISION_GROUP_PLAYER
				tr = util.TraceLine(trace)
				if (tr.Fraction==1) then
				if v:IsPlayer() then
					BurnPlayer(v, 5, self.Owner, self.Entity)
				end
				end
			end
		end
	end

	if (math.random(0, 6) < 1 and self.Entity:WaterLevel() <= 0 and self.TimerFire and tr.HitWorld) then
		local fire = ents.Create("env_fire")
			fire:SetPos(self.Entity:GetPos() + Vector(math.random(-100, 100), math.random(-100, 100), 0))
			fire:SetKeyValue("StartDisabled","0")
			fire:SetKeyValue("health",math.random(17,23))
			fire:SetKeyValue("firesize",math.random(58,73))
			fire:SetKeyValue("fireattack","1")
			fire:SetKeyValue("ignitionpoint","0")
			fire:SetKeyValue("damagescale","2")
			fire:SetKeyValue("spawnflags",136)
				
			fire:SetOwner(self.Entity:GetNWEntity("owner"))
			fire:Spawn()
			fire:Fire("StartFire","","0")
	end
	
	if (self.Entity:GetDTBool(0) and self.OneTime) then
		self.OneTime = false
		self.Fire = true
		self.Entity:EmitSound(Sound("Weapon_FlareGun.Burn"))
		self.Entity:Fire("kill", "", 10)		
	end

	self.Entity:Extinguish()
end

/*---------------------------------------------------------
   Name: ENT:OnRemove()
---------------------------------------------------------*/
function ENT:OnRemove()
	self.Entity:StopSound("Weapon_FlareGun.Burn")
end